<template>
  <div ref="chart" class="chart"  :style="{height:height,width:width}"/>
</template>

<script>
import * as echarts from 'echarts'
import macarons from 'echarts/theme/macarons'


export default {
  // mixins: [resize],
  props: {
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    title: {
      type: String,
      default: ''
    },
    // subtext: {
    //   type: String,
    //   default: ''
    // },
    showData: {
      type: Array,
      default(showDataProp) {
        return [
          {value: 0, name: '获取数据出错'},
        ]
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    showData: {
      handler() {
        this.$nextTick(() => {
          this.initChart()
        })
      },
      deep: true
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {

      this.chart = echarts.init(this.$refs.chart, "macarons")

      this.chart.setOption({
        title: {
          text: this.title,
          subtext: this.subtext,
          left: 'center',
          // padding: [1, 10]
        },
        tooltip: {
          trigger: 'item',
          formatter: '{b} <br/> {c} 篇，占比 {d}%'
        },
        legend: {
          // 图例较多可滚动
          type: 'scroll',
          // 垂直图例
          orient: 'vertical',
          right: '5%',
          top: '10%',
          bottom: '5%',
          // bottom: '0%',
          // left: 'right'
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            top: "0%",
            left: "0%",
            right: "15%",
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: false,
                fontSize: 20,
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data: this.showData
          }
        ]
      })

      window.addEventListener("resize", () => {
        // console.log("浏览器大小改变")
        this.chart.resize()
      })
    }
  }
}
</script>

<style scoped>
.chart {
  padding: 15px 0px;
  border: 1px solid #eeeeee;
  box-shadow: rgba(123, 123, 129, 0.2) 0px 7px 20px 0px;
}

</style>
